
//{{{ file documentation -------------------------------------------------------

/*!
 * @file   preferences.cpp
 * @brief  dicodor preferences window class implementation
 */

//}}} --------------------------------------------------------------------------
//{{{ includes / predeclarations -----------------------------------------------

// @(#) $Id:$

#include "preferences.h"
#include "colorselectbutton.h"
//#include "moc_preferences.cpp"	moc V5 sera incompatible V4
#include "ui_preferences.h"

//}}} --------------------------------------------------------------------------
//{{{ Preferences --------------------------------------------------------------
//}}} --------------------------------------------------------------------------
//{{{ .Preferences (QWidget * parent) -----------------------------------------

Preferences::Preferences (QWidget * parent) :
	QDialog (parent),
	ui (new Ui::Preferences)
{
	m_config = new Config (0);
	ui->setupUi (this) ;
	setWindowTitle (tr ("Préférences")) ;

	initIcons ();
	initGui ();
	initFields ();
	initConnect ();
}

//}}} --------------------------------------------------------------------------
//{{{ .~Preferences() ----------------------------------------------------------

Preferences::~Preferences()
{
	delete ui;
	delete m_config;
}

//}}} --------------------------------------------------------------------------
//{{{ .void initIcons() --------------------------------------------------------

void Preferences::initIcons()
{
	ui->clearPersonalDic     ->setIcon (QIcon::fromTheme ("user-trash" ) ) ;
	ui->clearTranslateDic     ->setIcon (QIcon::fromTheme ("user-trash" ) ) ;
	ui->mergePersonalDic     ->setIcon (QIcon::fromTheme ("document-open")) ;
	ui->removeLineFromPersonalDic ->setIcon (QIcon::fromTheme ("edit-clear" ) ) ;
	ui->removeLineFromTranslateDic->setIcon (QIcon::fromTheme ("edit-clear" ) ) ;
	ui->Ok            ->setIcon (QIcon::fromTheme ("window-close") ) ;
	setWindowIcon (QIcon::fromTheme ("preferences-desktop")) ;
}

//}}} --------------------------------------------------------------------------
//{{{ .void initGui () ------------------------------------------

void Preferences::initGui ()
{
	m_csButton = new ColorSelectButton (Qt::red, this);
	ui->colorLayout->addWidget (m_csButton);

	QButtonGroup* group = new QButtonGroup(this);
	group->setExclusive (true);

	group->addButton (ui->rbDicodor, 0);
	group->addButton (ui->rbOrtho, 1);
	group->addButton (ui->rbDic, 2);
	group->addButton (ui->rbTranslate, 3);

	connect (group,
			 SIGNAL(buttonClicked(int)),
			 ui->tabs,
			 SLOT(setCurrentIndex(int)));

	ui->rbDicodor->setChecked (true);
	ui->tabs->setCurrentIndex (0);
}

//}}} --------------------------------------------------------------------------
//{{{ .void initFields () ------------------------------------------

void Preferences::initFields ()
{
	// options de dicodor
	m_csButton->setColor (m_config->optionUnderlineColor ());
	ui->saveFormat->clear ();
	ui->saveFormat->addItems (m_config->supportedDocumentFormats ());
	int index = ui->saveFormat->findText (m_config->optionSaveFormat ());
	ui->saveFormat->setCurrentIndex (index);


	// Options d'Ortho
//    ui->languages->addItems (m_ortho->availableLanguages ());
//    ui->languages->setCurrentIndex (ui->languages->findText (m_ortho->currentLanguage ()));
//    ui->optionDistanceMax->setValue (m_ortho->distanceMax ());
//    ui->optionLengthMin->setValue (m_ortho->optionLengthMin ());
//    ui->optionLengthMin->setMinimum (2);
//    ui->optionWeirdMax->setValue (m_ortho->weirdMax ());
//    ui->optionSuggestMax->setValue (m_ortho->optionSuggestMax ());
//    ui->optionSuggestMax->setMinimum (2);

	// gestion du dico perso
	ui->personalDicList->clear ();
//	QStringList list (m_ortho->personalDic ());

//	ui->clearPersonalDic->setEnabled (!list.isEmpty ());
//	ui->removeLineFromPersonalDic->setEnabled (!list.isEmpty ());

//	if (!list.isEmpty ()) {
//		ui->personalDicList->addItems (list);
//		ui->personalDicList->setCurrentRow (0);
//	}

	// gestion du remplacement automatique
	ui->translateDicList->clear ();
//	list = m_ortho->translateDicAsList ();

//	ui->clearTranslateDic->setEnabled (!list.isEmpty ());
//	ui->removeLineFromTranslateDic->setEnabled (!list.isEmpty ());

//	if (!list.isEmpty ()) {
//		ui->translateDicList->addItems (list);
//		ui->translateDicList->setCurrentRow (0);
//	}
}

void Preferences::initConnect ()
{

	// initialisation des connexions SIGNAL-SLOT

//	connect (ui->removeLineFromPersonalDic,
//			 SIGNAL(clicked()),
//			 this,
//			 SLOT(onRemoveLineFromPersonalDic()));

//	connect (ui->clearPersonalDic,
//			 SIGNAL(clicked()),
//			 this,
//			 SLOT(onClearPersonalDic()));

//	connect (m_csButton,
//			 SIGNAL(colorChanged(QColor)),
//			 m_config,
//			 SLOT(setOptionUnderlineColor(QColor)));

//	connect (ui->saveFormat,
//			 SIGNAL(currentIndexChanged(QString)),
//			 m_config,
//			 SLOT(setOptionSaveFormat(QString)));

}


//}}} --------------------------------------------------------------------------
//{{{ .void changeEvent (QEvent * e) ------------------------------------------

void Preferences::changeEvent (QEvent * e)
{
	QDialog::changeEvent (e) ;
	switch (e->type())
	{
	case QEvent::LanguageChange:
		ui->retranslateUi (this) ;
		break;
	default:
		break;
	}
}

//}}} --------------------------------------------------------------------------
//{{{ .void on_Ok_clicked() ----------------------------------------------------

void Preferences::on_Ok_clicked()
{
	close ();
}

void Preferences::onRemoveLineFromPersonalDic()
{
	QString line (ui->personalDicList->currentItem ()->text ());
	if (Tools::userAgrees (tr ("Veuillez confirmer le retrait de \"%1\"\ndu dictionnaire personnel").arg (line))) {
//        m_ortho->removeLineFromPersonalDic (line);
	}
	initFields ();
}

void Preferences::onRemoveLineFromTranslatelDic()
{
	QString line (ui->translateDicList->currentItem ()->text ());
	if (Tools::userAgrees (tr ("Veuillez confirmer le retrait de cette correction automatique").arg (line))) {
//        m_ortho->removeLineFromTranslateDic (line);
	}
	initFields ();
}

void Preferences::onClearPersonalDic()
{
	if (Tools::userAgrees (tr ("Veuillez confirmer l'effacement complet du dictionnaire personnel"))) {
//        m_ortho->clearPersonalDic ();
	}
	initFields ();
}

//}}} --------------------------------------------------------------------------

